<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in rouls" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserId } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      rouls: []
    }
  },
  created() {
    this.loadRoles()
    this.loadRoleIds()
  },
  methods: {
    closeDialog() {
      this.$emit('close')
    },
    async loadRoles() {
      try {
        const res = await getRoles()
        // console.log(res)
        this.rouls = res.data.rows
      } catch (e) {
        console.log(e)
      }
    },
    async loadRoleIds() {
      try {
        const res = await getUserId(this.id)
        // console.log(res)
        this.roleIds = res.data.roleIds
      } catch (e) {
        console.log(e)
      }
    },
    resetIds() {
      this.roleIds = []
    },
    async hSubmit() {
      try {
        const res = await assignRoles({
          id: this.id,
          roleIds: this.roleIds
        })
        // 提醒用户
        this.$message.success(res.message)
        // 关闭 dialog
        this.$emit('close')
      } catch (e) {
        this.$message.error(e.message)
      }
    }
  }
}
</script>
